﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DB_NH.DB_Model.ZJK
{
    /// <summary>
    /// 专家分类
    /// </summary>
    [Serializable]
    public class ZJ_category : Entity
    {
        #region 数据表名称
        public static readonly string TableName = "zj_category";
        #endregion

        #region 数据表字段结构
        public struct Columns
        {
            //ID
            public static readonly string id = @"id";

            /// <summary>
            /// 分类代码
            /// </summary>
            public static readonly string category_code = @"category_code";

            /// <summary>
            /// 分类名称
            /// </summary>
            public static readonly string category_name = @"category_name";

            /// <summary>
            /// 上一级分类ID
            /// </summary>
            public static readonly string pre_category_id = @"pre_category_id";

            /// <summary>
            /// 分类级别
            /// </summary>
            public static readonly string category_level = @"category_level";

            /// <summary>
            /// 分类路径
            /// </summary>
            public static readonly string category_path = @"category_path";

            /// <summary>
            /// 是否叶子节点
            /// </summary>
            public static readonly string is_leaf = @"is_leaf";
        }
        #endregion

        #region 字段

        /// <summary>
        /// 分类代码
        /// </summary>
        public virtual string category_code { get; set; }

        /// <summary>
        /// 分类名称
        /// </summary>
        public virtual string category_name { get; set; }


        /// <summary>
        /// 上一级分类
        /// </summary>
        public virtual ZJ_category pre_category { get; set; }

        /// <summary>
        /// 下一级分类
        /// </summary>
        public virtual IList<ZJ_category> sub_categorys { get; set; }

        /// <summary>
        /// 分类级别
        /// </summary>
        public virtual int category_level { get; set; }

        /// <summary>
        /// 是否叶子节点
        /// </summary>
        public virtual int is_leaf { get; set; }

        /// <summary>
        /// 分类路径，由各级id组成，例如：xxx/yyy/zzz
        /// </summary>
        public virtual string category_path { get; set; }

        /// <summary>
        /// 当前分类的专家列表
        /// </summary>
        public virtual IList<DB_Model.ZJK.ZJ_expert> zj_experts { get; set; }
        #endregion

        public ZJ_category()
        {
            this.zj_experts = new List<ZJ_expert>();
            this.sub_categorys = new List<ZJ_category>();
        }

        /// <summary>
        /// 添加当前专家分类的子分类
        /// </summary>
        /// <param name="_child"></param>
        public virtual void addChild(ZJ_category _child)
        {
            _child.pre_category = this;
            _child.category_level = this.category_level + 1;
            _child.category_path = this.category_path + "/" + _child.Id;
            this.sub_categorys.Add(_child);
        }

        /// <summary>
        /// 删除当前节点记录
        /// </summary>
        public virtual void delete()
        {
            this.IsValid = 0;
            this.category_path = string.Empty;

            //删除当前专家类型的专家关联
            foreach (var _item in this.zj_experts)
            {
                _item.zj_categorys.Remove(this);
                //this.zj_experts.Remove(_item);
            }
            this.zj_experts.Clear();

            //当前类型节点记录不为空，删除父节点的子节点的自己
            if (this.pre_category != null)
            {
                this.pre_category = null;
                //this.pre_category.sub_categorys.Remove(this);
            }

            //同时删除当前节点下的所有子节点
            foreach (var _item in this.sub_categorys)
            {
                _item.delete();
            }
        }
    }
}
